/**
 *
 */
package susano.demo.service.company;

import java.util.List;

import susano.common.util.BeanMap;
import susano.common.util.EntityUtil;
import susano.datastore.manager.DatastoreManager;
import susano.demo.model.company.Company;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

/**
 *
 * @author tanaka.akira.2006
 *
 */
public class CompanyService {

	protected DatastoreManager dm = new DatastoreManager();

	public List<BeanMap> findAll() {
		User user = UserServiceFactory.getUserService().getCurrentUser();
		String email = null;
		if (user != null) {
			email = user.getEmail();
		}
		Query query = new Query(Company.class.getSimpleName());
		query.addSort("date", SortDirection.DESCENDING);
		query.addFilter("userId", FilterOperator.EQUAL, email);
		List<Entity> entities = dm.asList(query);
		return EntityUtil.createBeanMaps(entities);
	}

	public boolean put(BeanMap company) {
		User user = UserServiceFactory.getUserService().getCurrentUser();
		String email = null;
		if (user != null) {
			email = user.getEmail();
		}
		company.put("userId", email); // ユーザ固有のROOTエンティティを特定するためのキーとなる
		Entity entity = EntityUtil.createRootEntity(company, Company.class
				.getSimpleName());
		dm.put(entity);
		return dm.commit();
	}

}
